<template>
  <el-dialog model-value="dialogVisible" :title="dialogTitle" width="30%" @close="handleClose" class="el-dialog">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="130px">
      <el-form-item label="党员">
        <el-select v-model="form.partyId" class="widthForm" placeholder="请选择党员">
          <el-option v-for="item in SelectPartyList" :key="item.id" :label="item.nickname" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="宿舍星级" prop="domitoryGrade" :size="size">
        <el-input v-model="form.domitoryGrade" class="widthForm" type="number" :size="size" />
      </el-form-item>


      <el-form-item label="所获奖项数量" prop="prizeNum" :size="size">
        <el-input v-model="form.prizeNum" class="widthForm" type="number" :size="size" />
      </el-form-item>

      <el-form-item label="所获奖项" prop="prizeDes" :size="size">
        <el-input v-model="form.prizeDes" class="widthForm" type="textarea" :size="size" />
      </el-form-item>

      <el-form-item label="阅读书籍" prop="readNum" :size="size">
        <el-input v-model="form.readNum" class="widthForm" type="number" :size="size" />
      </el-form-item>

      <el-form-item label="服务时长" prop="serviceNum" :size="size">
        <el-input v-model="form.serviceNum" class="widthForm" type="number" :size="size" />
      </el-form-item>


    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, defineProps, ref, watch } from "vue";
import requestUtil, { getServerUrl } from "@/utils/request";
import { ElMessage } from "element-plus";

const tableData = ref([]);

const props = defineProps({
  id: {
    type: Number,
    default: -1,
    required: true,
  },
  dialogTitle: {
    type: String,
    default: "",
    required: true,
  },
  dialogVisible: {
    type: Boolean,
    default: false,
    required: true,
  },
});

// 日期
const size = (ref < "default") | "large" | ("small" > "default");

const form = ref({
  id: -1,
  partyId: "",
  domitoryGrade: 0,
  prizeNum: 0,
  prizeDes: "",
  readNum: 0,
  serviceNum: 0
});


const rules = ref({
  partyId: [
    { required: true, message: "请选择党员", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],

  domitoryGrade: [
    { required: true, message: "宿舍星级不能为空", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],
  prizeNum: [
    { required: true, message: "所获奖项数量不能为空", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],
  prizeDes: [
    { required: true, message: "所获奖项不能为空", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],
  readNum: [
    { required: true, message: "阅读书籍不能为空", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],
  serviceNum: [
    { required: true, message: "服务时长不能为空", trigger: "blur" },
    { required: true, trigger: "blur" },
  ],
});

const formRef = ref(null);

const initFormData = async (id) => {
  let data = { id };
  const res = await requestUtil.get("grades/getGradeById", data);
  form.value = res.data.grade;
};


// 用户信息下拉框
const SelectPartyList = ref([]);
const SelectPartyListFun = async () => {
  let data = {
    pageNum: 1,
    pageSize: 100,
  };
  const res = await requestUtil.post("party/partyList", data);
  SelectPartyList.value = res.data.partyList.records;
  console.log(SelectPartyList.value);
};

watch(
  () => props.dialogVisible,
  () => {
    let id = props.id;
    if (id != -1) {
      initFormData(id);
      SelectPartyListFun();
    } else {

      SelectPartyListFun();
      form.value = {
        id: -1,
        partyId: "",
        domitoryGrade: 0,
        prizeNum: 0,
        prizeDes: "",
        readNum: 0,
        serviceNum: 0
      };
    }
  }
);

const emits = defineEmits(["update:modelValue", "initUserList"]);

const handleClose = () => {
  emits("update:modelValue", false);
};

const handleConfirm = async () => {

  form.value.nickname = "";

  formRef.value.validate(async (valid) => {
    if (valid) {
      let result;
      if (form.value.id != -1) {
        result = await requestUtil.post("grades/updateDrades", form.value);
      } else {
        result = await requestUtil.post("grades/saveGrades", form.value);
      }

      let data = result.data;
      if (data.code == 200) {
        ElMessage.success(data.msg);
        formRef.value.resetFields();
        emits("initUserList");
        handleClose();
      } else {
        ElMessage.error(data.msg);
      }
    } else {
      console.log("fail");
    }
  });
};
</script>

<style scoped>
.widthForm {
  width: 100%;
}

.el-dialog {
  padding: 0 40px;
}

.demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
</style>
